import { array2Tree, tree2array } from '@/utils/tree';

/**
 * 检查节点的有效性
 * @param node
 * @param isDeleteInvalidNode 是否要删除掉无效节点
 * @param isStatisticsChildren 是否统计子节点
 * @param treeCheckStrictly 是否是严格模式, 影响统计结果
 * @returns {{disabled}|*}
 */
const checkNode = (node, isDeleteInvalidNode, isStatisticsChildren, treeCheckStrictly) => {
    if (!node) {
        return node;
    }
    // 深度优先, 递归检查节点
    if (node.children?.length) {
        node.children = node.children.map(item =>
            checkNode(item, isDeleteInvalidNode, isStatisticsChildren, treeCheckStrictly)
        );
    }
    if (node.isLeaf === false) {
        if (!node.children || node.children.filter(item => !item.disabled).length === 0) {
            node.disabled = true;
        }
        if (!node.children || node.children.length === 0 || node.children.filter(item => !item.invalid).length === 0) {
            node.invalid = true;
        }
        if (node.children?.length && isDeleteInvalidNode) {
            node.children = node.children.filter(item => !item.invalid);
        }
        if (isStatisticsChildren) {
            node.statistics = node.children?.length
                ? node.children.reduce((prev, curr) => {
                      if (!curr.isLeaf) {
                          prev += curr.statistics || 0;
                      } else {
                          prev += 1;
                      }
                      return prev;
                  }, 0)
                : 0;
            if (treeCheckStrictly) {
                node.statistics += node.children?.filter(item => item.isLeaf !== true)?.length || 0;
            }
        }
    }
    return node;
};

/**
 * 根据组件的需要, 对组件的可选属性做特殊处理
 * @param array
 * @param config
 * @returns {*}
 */
export const preprocessDataItem = (array, config) => {
    if (!array) {
        return [];
    }
    const {
        isNeedPermission,
        isDisableAuditData,
        isIgnoreOptionDisabled,
        isDeleteInvalidNode,
        isStatisticsChildren,
        treeCheckStrictly
    } = config || {};
    const hasResetAuditNodes = [];
    let result = array?.map(item => {
        const obj = item;
        // 如果没有数据权限，不可选择
        if (!obj.hasDataPermission && isNeedPermission === true) {
            // obj.checkable = false;
            obj.selectable = false;
            obj.disabled = true;
        }
        if (obj.auditStatus?.toString() !== '2' && isDisableAuditData === true) {
            // obj.checkable = false;
            obj.selectable = false;
            obj.disabled = true;
            hasResetAuditNodes.push(obj.id);
        }
        if (hasResetAuditNodes.includes(obj.pId) && isDisableAuditData === true) {
            // obj.checkable = false;
            obj.selectable = false;
            obj.disabled = true;
            hasResetAuditNodes.push(obj.id);
        }
        if (isIgnoreOptionDisabled) {
            obj.disabled = false;
        }
        if (obj?.children?.length) {
            obj.children = preprocessDataItem(obj.children, config);
        }
        return {
            ...obj
        };
    });
    const tree = array2Tree(result);
    const markedTree = tree.map(node => checkNode(node, isDeleteInvalidNode, isStatisticsChildren, treeCheckStrictly));
    result = tree2array(markedTree);
    return result;
};
